package excel;

import com.kis.model.channel.SendType;
import com.kis.service.commonService.CommonService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.usermodel.Sheet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanglong on 15-12-17.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
public class SendTypeImport {
    @Autowired
    private CommonService commonService;
    @Test
    public void testImp()throws Exception{
        InputStream is = new FileInputStream("/home/wanglong/work/data/wl.xls");
        List<SendType> sendTypes = getSendTypeData(is);
        commonService.batchAddObject(sendTypes);
    }
    public List<SendType> getSendTypeData(InputStream is) throws Exception{
        List<SendType> sendTypes = new ArrayList<>();
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet st = wb.getSheetAt(0);
        for (int i = 39; i < 62; i++) {
            String code = st.getRow(i).getCell(0).toString();
            String shortName= st.getRow(i).getCell(1).toString();
            String info= st.getRow(i).getCell(2).toString();
            sendTypes.add(new SendType(code,info,shortName));
        }
        return sendTypes;
    }
}
